"""
老王开枪游戏
"""


class Person(object):
    """人类"""
    def __init__(self, name):
        super().__init__()
        self.name = name  # 人名
        self.hp = 100
        self.__gun = None

    def __str__(self):
        if self.__gun:
            return "%s有枪，血量%s，%s" % (self.name, self.hp, self.__gun)
        else:
            if self.hp > 0:
                return "%s血量为%s" % (self.name, self.hp)
            else:
                return "%s已经挂了..." % self.name

    @staticmethod
    def install_bullet(clip, bullet):
        """把子弹安装到弹夹中"""
        clip.pull_bullet(bullet)

    @staticmethod
    def install_clip(gun, clip):
        """把弹夹安装到枪中"""
        gun.pull_clip(clip)

    def pickup_gun(self, gun):
        """拿枪"""
        self.__gun = gun

    def shoot(self, enemy):
        """开枪打敌人"""
        # 枪开火
        self.__gun.fire(enemy)


class Gun(object):
    """枪类"""
    def __init__(self, name):
        super().__init__()
        self.name = name  # 枪的名称
        self.__clip = None  # 弹夹

    def __str__(self):
        if self.__clip:
            return "枪的信息：%s，%s" % (self.name, self.__clip)
        else:
            return "枪的信息：%s，没有弹夹..." % self.name

    def pull_clip(self, clip):
        """把弹夹安装到枪中"""
        if self.__clip is None:
            self.__clip = clip

    def fire(self, enemy):
        """开火"""
        # 取出子弹
        bullet = self.__clip.poll_bullet()

        if bullet:
            bullet.kill(enemy)
        else:
            print("没有子弹了...")


class Clip(object):
    """弹夹类"""
    def __init__(self, max):
        super().__init__()
        self.max = max  # 弹夹的最大容量
        self.__bullets = []  # 存放子弹

    def __str__(self):
        return "弹夹的信息：%d/%d" % (len(self.__bullets), self.max)

    def pull_bullet(self, bullet):
        """安装子弹"""
        if len(self.__bullets) <= self.max:
            self.__bullets.append(bullet)

    def poll_bullet(self):
        """取出一颗子弹"""
        if self.__bullets:
            return self.__bullets.pop()
        else:
            return None


class Bullet(object):
    """子弹类"""
    def __init__(self, power):
        super().__init__()
        self.power = power  # 杀伤力

    def kill(self, enemy):
        """开枪"""
        # 掉血
        enemy.hp -= self.power


def main():
    """控制整个程序流程"""
    print("*" * 15 + "老王开枪游戏开始" + "*" * 15)
    # 1. 创建老王对象
    laowang = Person("老王")

    # 2. 创建一个枪对象
    ak47 = Gun("AK47")

    # 3. 创建一个弹夹对象
    clip = Clip(20)

    # 4. 创建一些子弹
    for i in range(15):
        bullet = Bullet(10)

        # 5. 老王把子弹安装到弹夹中
        laowang.install_bullet(clip, bullet)

    # 6. 老王把弹夹安装到枪中
    laowang.install_clip(ak47, clip)

    # 7. 老王拿枪
    laowang.pickup_gun(ak47)
    print(laowang)

    # 7. 老王开枪打敌人
    enemy = Person("隔壁老宋")
    for i in range(20):
        print("老王开了第%d枪..." % i)
        laowang.shoot(enemy)
        print(enemy)

    print(laowang)
    print(enemy)
    print("*" * 15 + "老王开枪游戏结束" + "*" * 15)


if __name__ == "__main__":
    main()